package com.xmg.day08.reflect;


import java.util.stream.Stream;

public class IOCTest {
    public static void main(String[] args) {
        UserController userController = new UserController();
        Class<? extends UserController> clazz = userController.getClass();
        //获取所有的属性值
        Stream.of(clazz.getDeclaredFields()).forEach(field -> {
            //遍历每一个属性值，如果发现被Autowired注解，则进行注入
            Autowired autowired = field.getAnnotation(Autowired.class);
            if(autowired!=null){
                //设置私有属性值也可以被访问
                field.setAccessible(true);
                //获取属性的类型，方便创建具体对象
                Class<?> type = field.getType();
                try {
                    Object instance = type.newInstance();
                    field.set(userController,instance);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

            }
        });

        System.out.println(userController.getUserService());
    }
}
